{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/152_extract_timeseries_to_points.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install -U geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "## Specify Latitude, Longitude, & Date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the latitude and longitude\n",
    "lat = 28.60\n",
    "lon = 77.22\n",
    "\n",
    "# Define time range\n",
    "start_date = \"2000-01-01\"\n",
    "end_date = \"2015-12-31\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "## Extract ERA5-Land Daily Timeseries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize image collection\n",
    "image_collection = ee.ImageCollection(\"ECMWF/ERA5_LAND/DAILY_AGGR\")\n",
    "\n",
    "# Define bands of interest (must match available band names exactly)\n",
    "band_names = [\"temperature_2m_min\", \"temperature_2m_max\", \"total_precipitation_sum\"]\n",
    "\n",
    "# Define the scale\n",
    "scale = 11132\n",
    "\n",
    "# Extract time series at specified latitude and longitude\n",
    "result = geemap.extract_timeseries_to_point(\n",
    "    lat=lat,\n",
    "    lon=lon,\n",
    "    image_collection=image_collection,\n",
    "    band_names=band_names,\n",
    "    start_date=start_date,\n",
    "    end_date=end_date,\n",
    "    scale=scale,\n",
    ")\n",
    "\n",
    "# Preview results\n",
    "print(result.shape)\n",
    "result.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "## Extract CHIRPS Daily Precipitation Timeseries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize image collection\n",
    "image_collection = ee.ImageCollection(\"UCSB-CHG/CHIRPS/DAILY\")\n",
    "\n",
    "# Define the band name\n",
    "band_names = [\"precipitation\"]\n",
    "\n",
    "# Define the scale\n",
    "scale = 5566\n",
    "\n",
    "# Extract time series at specified latitude and longitude\n",
    "result = geemap.extract_timeseries_to_point(\n",
    "    lat=lat,\n",
    "    lon=lon,\n",
    "    image_collection=image_collection,\n",
    "    band_names=band_names,\n",
    "    start_date=start_date,\n",
    "    end_date=end_date,\n",
    "    scale=scale,\n",
    ")\n",
    "\n",
    "# Preview results\n",
    "print(result.shape)\n",
    "result.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "## Extract MODIS Terra Vegetation Indices Timeseries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize image collection\n",
    "image_collection = ee.ImageCollection(\"MODIS/061/MOD13Q1\")\n",
    "\n",
    "# Define the band names\n",
    "band_names = [\"NDVI\", \"EVI\"]\n",
    "\n",
    "# Define the scale\n",
    "scale = 250\n",
    "\n",
    "# Extract time series at specified latitude and longitude\n",
    "result = geemap.extract_timeseries_to_point(\n",
    "    lat=28.60,\n",
    "    lon=77.22,\n",
    "    image_collection=image_collection,\n",
    "    band_names=band_names,\n",
    "    start_date=start_date,\n",
    "    end_date=end_date,\n",
    "    scale=scale,\n",
    ")\n",
    "\n",
    "# Preview results\n",
    "print(result.shape)\n",
    "result.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13",
   "metadata": {},
   "source": [
    "## Plot the Timeseries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Simple plot\n",
    "plt.figure(figsize=(12, 5))\n",
    "\n",
    "for band in [\"NDVI\", \"EVI\"]:\n",
    "    plt.plot(\n",
    "        result[\"time\"], result[band] * 0.0001, label=band\n",
    "    )  # apply scale factor of 0.0001\n",
    "\n",
    "plt.title(f\"NDVI & EVI Time Series at {lat, lon}\")\n",
    "plt.xlabel(\"Date\")\n",
    "plt.ylabel(\"Value\")\n",
    "plt.legend()\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "geo"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
